/**
 * @description 分组服务类
 * @author AI Assistant
 * @date 2023-10-27
 */

import { BaseService } from './base'
import { STORAGE_KEYS, storage } from '@/utils/storage'
import { Group } from '@/models/group'

export class GroupService extends BaseService {
  constructor() {
    super(STORAGE_KEYS.GROUPS)
  }

  create(data) {
    const group = new Group(data)
    return super.create(group.toJSON())
  }

  // 根据清单ID获取分组列表
  getGroupsByListId(listId) {
    const groups = this.getAll()
    return groups.filter(group => group.listId === listId)
      .sort((a, b) => a.order - b.order)
  }

  // 更新分组排序
  updateOrder(groupIds) {
    const groups = this.getAll()
    let updated = false

    groupIds.forEach((id, index) => {
      const groupIndex = groups.findIndex(group => group.id === id)
      if (groupIndex > -1) {
        groups[groupIndex].order = index
        groups[groupIndex].updateTime = Date.now()
        updated = true
      }
    })

    if (updated) {
      storage.set(this.storageKey, groups)
    }
    return updated
  }
} 